package com.example.wisdomclassroomapi.domain.repository;

import com.example.wisdomclassroomapi.domain.entity.ScheduleLessonEntity;
import com.example.wisdomclassroomapi.infrastructure.mapper.ScheduleLessonMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
/**
 * @Package: com.example.wisdomclassroomapi.domain.repository
 * @Description: <>
 * @Author: scholes
 * @CreateDate: 2022-04-19
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-19
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Repository
public class ScheduleLessonRepository {
    @Resource
    private ScheduleLessonMapper scheduleLessonMapper;

    public void add(ScheduleLessonEntity entity) {
       scheduleLessonMapper.insert(entity);
    }

    public ScheduleLessonEntity findById(String id) {
        return scheduleLessonMapper.selectById(id);
    }

    public int updateById(ScheduleLessonEntity entity) {
        return scheduleLessonMapper.updateById(entity);
    }

    public int deleteById(String id) {
        return scheduleLessonMapper.deleteById(id);
    }

    public IPage<ScheduleLessonEntity> page(ScheduleLessonEntity entity,IPage page) {
        QueryWrapper<ScheduleLessonEntity> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(entity.getOutlineId())){
            wrapper.eq("outline_id",entity.getOutlineId());
            }
            if (StringUtils.isNotBlank(entity.getLessonId())){
            wrapper.eq("lesson_id",entity.getLessonId());
            }
            if (entity.getStartTime()!=null){
            wrapper.eq("start_time",entity.getStartTime());
            }
            if (entity.getEndTime()!=null){
            wrapper.eq("end_time",entity.getEndTime());
            }
        return scheduleLessonMapper.selectPage(page, wrapper);
    }

    public List<ScheduleLessonEntity> list(ScheduleLessonEntity entity) {
        QueryWrapper<ScheduleLessonEntity> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(entity.getOutlineId())){
            wrapper.eq("outline_id",entity.getOutlineId());
            }
            if (StringUtils.isNotBlank(entity.getLessonId())){
            wrapper.eq("lesson_id",entity.getLessonId());
            }
            if (entity.getStartTime()!=null){
                wrapper.eq("start_time",entity.getStartTime());
            }
            if (entity.getEndTime()!=null){
                wrapper.eq("end_time",entity.getEndTime());
            }
        return scheduleLessonMapper.selectList(wrapper);
    }

}